//
//  Headview.m
//  QQ_Youngsher
//
//  Created by Maxwell on 16/1/14.
//  Copyright © 2016年 LLLTeam. All rights reserved.
//

#define SCREEN_W [[UIScreen mainScreen] bounds].size.width
#define SCREEN_H [[UIScreen mainScreen] bounds].size.height

#define BTN_W 64
#define BTN_H 70

#import "Headview.h"

@implementation Headview

- (instancetype)initWithFrame:(CGRect)frame imageArray:(NSMutableArray *)imageArray titleArray:(NSMutableArray *)titleArray{
    
    float distance = (SCREEN_W - BTN_W * 4) / 5;
    
    self = [super initWithFrame:frame];
    if (self) {
        
        UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        searchBtn.frame = CGRectMake(5, 5, SCREEN_W - 10, 25);
        searchBtn.backgroundColor = [UIColor colorWithRed:0.8627 green:0.8627 blue:0.8627 alpha:1.0];
        [searchBtn setTitle:@"搜索" forState:UIControlStateNormal];
        [searchBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        searchBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        searchBtn.tag = 0;
        [searchBtn addTarget:self action:@selector(clickHeadViewBtn:) forControlEvents:UIControlEventTouchUpInside];
        searchBtn.layer.cornerRadius = 4.0;
        searchBtn.layer.masksToBounds = YES;
        [self addSubview:searchBtn];
        
        for (int i = 0 ; i < imageArray.count; i++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(distance * (i + 1) + BTN_W * i, 40, BTN_W, BTN_H);
            btn.tag = i+1;
            [btn addTarget:self action:@selector(clickHeadViewBtn:) forControlEvents:UIControlEventTouchUpInside];
            
            btn.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
            btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
            
            NSString *imageName = [NSString stringWithFormat:@"%@",imageArray[i]];
            UIImage *image = [UIImage imageNamed:imageName];
            [btn setImage:image forState:UIControlStateNormal];
            btn.imageEdgeInsets = UIEdgeInsetsMake(10, (BTN_W - image.size.width) / 2, 0, 0);
            
            NSString *title = [NSString stringWithFormat:@"%@",titleArray[i]];
            [btn setTitle:title forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:14.0];
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            
            NSDictionary *attribute = @{NSFontAttributeName: btn.titleLabel.font};
            CGRect titelRect = [title boundingRectWithSize:CGSizeMake(10000, 20) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil];
            btn.titleEdgeInsets = UIEdgeInsetsMake(20 + image.size.height, (BTN_W - titelRect.size.width) / 2 - image.size.width, 0, 0);
            
            [self addSubview:btn];
            
        }
        UIView *endView = [[UIView alloc] initWithFrame:CGRectMake(0, frame.size.height - 20, SCREEN_W, 20)];
        endView.backgroundColor = [UIColor colorWithRed:0.8353 green:0.8353 blue:0.8353 alpha:1.0];
        [self addSubview:endView];
    }
    return self;
}

#pragma mark - HeadViewDelegate
- (void)clickHeadViewBtn:(UIButton *)btn{
    [self.delegate clickHeadViewBtn:btn];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
